package com.teradata.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Spring中的做法，就是用注册式的单例
 *
 */
public class BeanFactory {
    private BeanFactory(){}

    private static Map<String, Object> ioc = new ConcurrentHashMap<String, Object>();

    public synchronized static Object getBean(String className){
        if (!ioc.containsKey(className)){
            Object obj = null;
            try {
                obj = Class.forName(className).newInstance();
                ioc.put(className, obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return obj;
        } else {
            return ioc.get(className);
        }
        /*if (name == null){
            name = BeanFactory.class.getName();
        }

        if (ioc.get(name) == null) {
            try {
                ioc.put(name, new BeanFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return (BeanFactory) ioc.get(name);*/
    }


}
